package club.adger.controller;

import club.adger.entity.User;
import club.adger.entity.UserList;
import club.adger.entity.UserMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: Adger
 * @Date: 2020/08/21/21:44
 */
@RestController
@RequestMapping("/data")
public class DataBindHandler {
    @RequestMapping("/baseType")
    public String baseType(int id){
        return id+"";
    }

    @RequestMapping("/packageType")
    public String packageType(@RequestParam(value = "num",required = false,defaultValue = "0") Integer id){
        return id+"";
    }

    @RequestMapping("/array")
    public String array(String[] name){
        String s = Arrays.toString(name);
        return s;
    }

    @RequestMapping("/list")
    public String list(UserList userList){
        StringBuffer stringBuffer = new StringBuffer();
        for (User user:userList.getUsers()) {
            stringBuffer.append(user);
        }
        return stringBuffer.toString();
    }

    @RequestMapping("/map")
    public String map(UserMap userMap){
        StringBuffer stringBuffer = new StringBuffer();
        // map集合遍历跟List集合不一样 遍历key值
        for (String key : userMap.getUsers().keySet()){
            User user = userMap.getUsers().get(key);
            stringBuffer.append(user);
        }
        return stringBuffer.toString();
    }

    /**
     * 如果你是json数据对象 需要加@RequestBody 一个 是@ResponseBody响应 一个是接受
     * @param user
     * @return
     */
    @RequestMapping("/json")
    public User json(@RequestBody User user){
        System.out.println(user);
        return user;
    }
}
